/*
 * Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.huawei.cloudphone.virtualdevice.sensor;

import static com.huawei.cloudphone.jniwrapper.JNIWrapper.SENSOR_DATA;
import static com.huawei.cloudphone.virtualdevice.common.VirtualDeviceProtocol.MSG_HEADER_LEN;

import android.hardware.SensorManager;
import android.util.Log;

import com.huawei.cloudphone.virtualdevice.common.IVirtualDeviceDataListener;
import com.huawei.cloudphone.virtualdevice.common.VirtualDeviceManager;
import com.huawei.cloudphone.virtualdevice.common.VirtualDeviceProtocol;
import com.huawei.cloudphone.virtualdevice.common.VirtualDeviceProtocol.MsgHeader;

public class VirtualSensorManager extends VirtualDeviceManager {
    private static final String TAG = "VirtualSensorManager";

    public static final short DEV_ACCELERATION = 0x0;
    public static final short DEV_GYROSCOPE = 0x1;
    public static final short OPT_SENSOR_ENABLE_REQ = 0x1;
    public static final short OPT_SENSOR_ENABLE_RSP = 0x1001;
    public static final short OPT_SENSOR_DISABLE_REQ = 0x2;
    public static final short OPT_SENSOR_DISABLE_RSP = 0x1002;
    public static final short OPT_SENSOR_DATA = 0x10;

    private VirtualSensor mVirtualSensor;
    private VirtualDeviceProtocol mVirtualDeviceProtocol;

    public VirtualSensorManager(VirtualDeviceProtocol virtualDeviceProtocol, SensorManager sensorManager) {
        mVirtualDeviceProtocol = virtualDeviceProtocol;
        mVirtualSensor = new VirtualSensor(sensorManager);
    }

    public void processMsg(MsgHeader header, byte[] body) {
        switch (header.mOptType) {
            case OPT_SENSOR_ENABLE_RSP:
                Log.i(TAG, "processMsg: enable sensor");
                mVirtualSensor.registerSensorDataListener(new SensorDataListener());
                mVirtualSensor.startProcess();
                break;
            case OPT_SENSOR_DISABLE_RSP:
                Log.i(TAG, "processMsg: disable sensor");
                mVirtualSensor.stopProcess();
                break;
            default:
                Log.e(TAG, "processMsg: error opt type");
        }
    }

    public void start() {
        int rspMsgLen = MSG_HEADER_LEN;
        MsgHeader header = new MsgHeader(OPT_SENSOR_ENABLE_REQ, DEV_TYPE_SENSOR, DEV_GYROSCOPE, rspMsgLen);
        mVirtualDeviceProtocol.sendMsg(header, null, SENSOR_DATA);
        header = new MsgHeader(OPT_SENSOR_ENABLE_REQ, DEV_TYPE_SENSOR, DEV_ACCELERATION, rspMsgLen);
        mVirtualDeviceProtocol.sendMsg(header, null, SENSOR_DATA);
    }

    public void stop() {
        int rspMsgLen = MSG_HEADER_LEN;
        MsgHeader header = new MsgHeader(OPT_SENSOR_DISABLE_REQ, DEV_TYPE_SENSOR, DEV_GYROSCOPE, rspMsgLen);
        mVirtualDeviceProtocol.sendMsg(header, null, SENSOR_DATA);
        header = new MsgHeader(OPT_SENSOR_DISABLE_REQ, DEV_TYPE_SENSOR, DEV_ACCELERATION, rspMsgLen);
        mVirtualDeviceProtocol.sendMsg(header, null, SENSOR_DATA);
    }

    class SensorDataListener implements IVirtualDeviceDataListener {
        @Override
        public void onRecvData(Object... args) {
            String xStr = Float.valueOf((float) args[0]).toString();
            String yStr = Float.valueOf((float) args[1]).toString();
            String zStr = Float.valueOf((float) args[2]).toString();
            String accurateStr = Integer.valueOf((int) args[3]).toString();
            String body = xStr + ":" + yStr + ":" + zStr + ":" + accurateStr + ":";
            int type = (int) args[4];
            int bodyLen = body.getBytes().length;
            int rspMsgLen = bodyLen + MSG_HEADER_LEN;
            MsgHeader header = new MsgHeader(OPT_SENSOR_DATA, DEV_TYPE_SENSOR, (short)type, rspMsgLen);
            byte[] rspBody = new byte[bodyLen];
            System.arraycopy(body.getBytes(), 0, rspBody, 0, bodyLen);
            mVirtualDeviceProtocol.sendMsg(header, rspBody, SENSOR_DATA);

        }
    }
}
